<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;
class Transfer extends Backend
{
    /**
     * 查看
     */
    public function index()
    {
        return $this->view->fetch();
    }

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 所有视频
     */
    public function videos()
    {
        $arr=Db::name('avideo')->order('weigh asc')->field('id,cate_id,name')->select()->toArray();
        if(!empty($arr)){
            foreach ($arr as $k=>$v){
                $arr[$k]['value']=$arr[$k]['id'];
                $arr[$k]['title']=$arr[$k]['name'];
                $arr[$k]['disabled']='';
                $arr[$k]['checked']='';
            }
        }
        $this->success('ok','',$arr);
    }
    public function cate_videos()
    {
        $cate_id=input('cate_id');
        $avideo=Db::name('avideo')->order('weigh asc')->field('id,cate_id')->select()->toArray();
        $arr=array();
        if(!empty($avideo)){
            foreach ($avideo as $k=>$v){
                $avideo[$k]['cate_id']=explode(',',$avideo[$k]['cate_id']);
                if(in_array($cate_id,$avideo[$k]['cate_id'])){
                    $arr[]=$avideo[$k]['id'];
                }
            }
        }
        $this->success('ok','',$arr);
    }
    public function tianjiavideos()
    {
        $ids=explode(',',input('ids'));
        $modules=input('modules');

        $avideo=Db::name('avideo')->whereIn('id',$ids)->field('id,cate_id')->order('weigh asc')->select()->toArray();
        if(!empty($avideo)){
            foreach ($avideo as $k=>$v){
                $avideo[$k]['cate_id']=explode(',',$avideo[$k]['cate_id']);
                if(!in_array($modules,$avideo[$k]['cate_id'])){
                    $avideo[$k]['cate_id'][]=$modules;
                    $data=array();
                    $data['cate_id']=implode(',',$avideo[$k]['cate_id']);
                    Db::name('avideo')->where('id',$avideo[$k]['id'])->update($data);
                    unset($data);
                }
            }
        }
        $this->success('ok','','');
    }
    public function quchuvideos()
    {
        $ids=explode(',',input('ids'));
        $modules=input('modules');
        $avideo=Db::name('avideo')->whereIn('id',$ids)->field('id,cate_id')->order('weigh asc')->select()->toArray();
        if(!empty($avideo)){
            foreach ($avideo as $k=>$v){
                $avideo[$k]['cate_id']=explode(',',$avideo[$k]['cate_id']);
                if(in_array($modules,$avideo[$k]['cate_id'])){
                    $key = array_search($modules,$avideo[$k]['cate_id']);
                    if($key===0||$key){
                        unset($avideo[$k]['cate_id'][$key]);
                    }
                    $data=array();
                    if(empty($avideo[$k]['cate_id'])){
                        $data['cate_id']='';
                    }else{
                        $data['cate_id']=implode(',',$avideo[$k]['cate_id']);
                    }
                    Db::name('avideo')->where('id',$avideo[$k]['id'])->update($data);
                    unset($data);
                }
            }
        }
        $this->success('ok','','');
    }
}